package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 根据用户ID查询用户基本信息
     *
     * @param userId
     * @return
     */
    @Operation(summary = "根据用户ID查询用户基本信息")
    @GetMapping("/userInfo/getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId) {
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }


    /**
     * 专辑详情中每页声音列表(非免费试听)，对提交专辑以及声音ID进行判断得出每个声音购买情况
     *
     * @param userId                       用户ID
     * @param albumId                      专辑ID
     * @param needCheckBuyStausTrackIdList 需要判断购买状态声音ID集合
     * @return {20158:1,20159:1}
     */
    @Operation(summary = "专辑详情中每页声音列表，对提交专辑以及声音ID进行判断得出每个声音购买情况")
    @PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
    public Result<Map<Long, Integer>> userIsPaidTrack(
            @PathVariable Long userId,
            @PathVariable Long albumId,
            @RequestBody List<Long> needCheckBuyStausTrackIdList
    ) {
        Map<Long, Integer> map = userInfoService.getCheckBuyStausTrackIdList(userId, albumId, needCheckBuyStausTrackIdList);
        return Result.ok(map);
    }


    /**
     * 验证当前用户是否已购指定专辑ID
     *
     * @param albumId
     * @return true:已购  false：未购
     */
    @GuiGuLogin
    @Operation(summary = "验证当前用户是否已购指定专辑ID")
    @GetMapping("/userInfo/isPaidAlbum/{albumId}")
    public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
        //1.获取用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.查询用户是否已购专辑
        Boolean isPaid = userInfoService.isPaidAlbum(userId, albumId);
        return Result.ok(isPaid);
    }


    /**
     * 查询用户某个专辑已购声音ID列表
     *
     * @param albumId
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "查询用户某个专辑已购声音ID列表")
    @GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
    public Result<List<Long>> getUserPaidTrackIdList(@PathVariable Long albumId) {
        //1.获取用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.根据用户ID+专辑ID查询已购声音列表列表
        List<Long> list = userInfoService.getUserPaidTrackIdList(userId, albumId);
        return Result.ok(list);
    }


    /**
     * 支付（余额，微信）成功后虚拟物品发货
     *
     * @param userPaidRecordVo
     * @return
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "支付（余额，微信）成功后虚拟物品发货")
    @PostMapping("/userInfo/savePaidRecord")
    public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
        //1.优先使用VO对象中用户ID
        if (userPaidRecordVo.getItemIdList() == null) {
            userPaidRecordVo.setUserId(AuthContextHolder.getUserId());
        }
        //2.调用业务逻辑虚拟物品发货（新增购买记录）
        userInfoService.savePaidRecord(userPaidRecordVo);
        return Result.ok();
    }


    /**
     * 更新VIP状态：处理过期会员
     * @return
     */
    @Operation(summary = "更新VIP状态：处理过期会员")
    @GetMapping("/updateVipExpireStatus")
    public Result updateVipExpireStatus(){
        userInfoService.updateVipExpireStatus(new Date());
        return Result.ok();
    }
}

